﻿using Abp.IdentityServer4;
using Abp.Runtime.Security;
using AVISOutBound.SSOServer.Authorization.Members;
using IdentityModel;
using IdentityServer4.Services;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Text;

namespace AVISOutBound.SSOServer.Identity
{
    public static class ABGIdentityServerBuilderExtensions
    {
        public static IIdentityServerBuilder AddABGIdentityServer<TUser>(this IIdentityServerBuilder builder, Action<AbpIdentityServerOptions> optionsAction = null)
            where TUser : Member
        {
            var options = new AbpIdentityServerOptions();
            optionsAction?.Invoke(options);

            builder.AddAspNetIdentity<TUser>();

            //builder.AddProfileService<AbpProfileService<TUser>>();
            //builder.AddResourceOwnerValidator<AbpResourceOwnerPasswordValidator<TUser>>();

            //builder.Services.Replace(ServiceDeskcriptor.Transient<IClaimsService, AbpClaimsService>());

            if (options.UpdateAbpClaimTypes)
            {
                AbpClaimTypes.UserId = JwtClaimTypes.Subject;
                AbpClaimTypes.UserName = JwtClaimTypes.Name;
                AbpClaimTypes.Role = JwtClaimTypes.Role;
            }

            if (options.UpdateJwtSecurityTokenHandlerDefaultInboundClaimTypeMap)
            {
                JwtSecurityTokenHandler.DefaultInboundClaimTypeMap[AbpClaimTypes.UserId] = AbpClaimTypes.UserId;
                JwtSecurityTokenHandler.DefaultInboundClaimTypeMap[AbpClaimTypes.UserName] = AbpClaimTypes.UserName;
                JwtSecurityTokenHandler.DefaultInboundClaimTypeMap[AbpClaimTypes.Role] = AbpClaimTypes.Role;
            }

            return builder;
        }
    }

    //public static class ABGIdentityServerBuilderExtensions
    //{
    //    public static IIdentityServerBuilder AddABGIdentityServer<TUser>(this IIdentityServerBuilder builder, Action<AbpIdentityServerOptions> optionsAction = null)
    //        where TUser : AbpUser<TUser>
    //    {
    //        var options = new AbpIdentityServerOptions();
    //        optionsAction?.Invoke(options);

    //        builder.AddAspNetIdentity<TUser>();

    //        builder.AddProfileService<AbpProfileService<TUser>>();
    //        builder.AddResourceOwnerValidator<AbpResourceOwnerPasswordValidator<TUser>>();

    //        builder.Services.Replace(ServiceDescriptor.Transient<IClaimsService, AbpClaimsService>());

    //        if (options.UpdateAbpClaimTypes)
    //        {
    //            AbpClaimTypes.UserId = JwtClaimTypes.Subject;
    //            AbpClaimTypes.UserName = JwtClaimTypes.Name;
    //            AbpClaimTypes.Role = JwtClaimTypes.Role;
    //        }

    //        if (options.UpdateJwtSecurityTokenHandlerDefaultInboundClaimTypeMap)
    //        {
    //            JwtSecurityTokenHandler.DefaultInboundClaimTypeMap[AbpClaimTypes.UserId] = AbpClaimTypes.UserId;
    //            JwtSecurityTokenHandler.DefaultInboundClaimTypeMap[AbpClaimTypes.UserName] = AbpClaimTypes.UserName;
    //            JwtSecurityTokenHandler.DefaultInboundClaimTypeMap[AbpClaimTypes.Role] = AbpClaimTypes.Role;
    //        }

    //        return builder;
    //    }
    //}
}
